package com.pat.web.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import com.pat.common.Constants;
import com.pat.web.ApiResponse;
import com.pat.web.NoNeedLogon;

public class FocusController extends Controller{
	private static final int PAGESIZE = 20;
@NoNeedLogon
	public void getFocusInfoList(){//
		Integer pageIndex = getParaToInt("pageIndex");
		if (pageIndex == null || pageIndex < 1) {
			pageIndex = 1;
		}
		Integer pageSize = getParaToInt("pageSize");
		if (pageSize == null) {
			pageSize = PAGESIZE;
		}
		
		Integer focusType = getParaToInt("focusType");//关注的类型  1：领养  2:求助   3:问题   4:回答
		
		Integer tableType = getParaToInt("tableType");//1表示我关注   2表示我的发布
		
		
		String selectSql = null ;
		String sqlExceptSelect = null;
		
		if (focusType == null || tableType == null ) {
			return ;
		}
		Long userId = getAttr(Constants.LOGIN_USER_ID);
		switch (focusType) {
		case 1:
			if (tableType == 1) {
				selectSql = "select a.id,a.name,a.age,a.weight,a.variety_id,a.category_id,a.brief,a.collection_num,a.pic1,a.gender,c.category_name ";
				sqlExceptSelect ="from adopt a,focus f,pet_category c where f.focus_type = 1 and a.category_id = c.id and a.id = f.focus_content_id and f.user_id = ? " ;
			}else if (tableType == 2) {
				selectSql = "select a.id,a.name,a.age,a.weight,a.variety_id,a.category_id,a.brief,a.collection_num,a.pic1,a.gender,c.category_name,f.focus_content_id ";
				sqlExceptSelect =" from (adopt a inner join  pet_category c on a.category_id = c.id) left join focus f on a.id = f.focus_content_id and f.focus_type = 1 and  f.user_id = a.user_id" 
						+ " where a.category_id = c.id and a.user_id = ? " ;
			}
			
			break;
			
		case 2:
			
			return;
			
			
		case 3:
			selectSql = "select a.* ";
			if (tableType == 1) {
				sqlExceptSelect ="from question_info a,focus f where f.focus_type = 3 and a.id = f.focus_content_id and f.user_id = ? " ;
			}else if (tableType == 2) {
				sqlExceptSelect ="from question_info a where a.user_id = ? "  ;

			}
			break;
			
		case 4:
			selectSql = "select a.id,a.content,a.user_id,u.nick_name,u.photo,a.agree_num,a.disagree_num,a.collection_num,a.comment_num,a.create_time,a.update_time,a.question_id,q.title,q.answer_num ";
			if (tableType == 1) {
				sqlExceptSelect ="from answer_info a,focus f,question_info q ,user u  where a.question_id = q.id  and u.id = f.user_id and f.focus_type = 4 and a.id = f.focus_content_id and f.user_id = ? " ;
			}else if (tableType == 2) {
				sqlExceptSelect ="from answer_info a ,question_info q ,user u where a.question_id = q.id and u.id = a.user_id and  a.user_id = ? ";

			}
			
			break;

		default:
			return ;
		}
		
		sqlExceptSelect = sqlExceptSelect + " order by a.create_time desc";
		ApiResponse response = new ApiResponse();
		response.setStatusCode("0000");
		
		Page<Record> records =  Db.paginate(pageIndex, pageSize,selectSql,sqlExceptSelect,userId);
		response.setData(records);
		render(new JsonRender(response));
	}
	
}
